package com.example.discover.view

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.dp
import com.example.discover.DiscoverFeatureSDK
import com.example.discover.R
import com.example.discover.viewmodel.DiscoveryListState
import com.example.featuresdk.theme.DemoTheme
import com.example.featuresdk.view.*
import java.lang.Integer.max


@ExperimentalFoundationApi
@ExperimentalUnitApi
@Composable
fun DiscoverListScreen(uiState: DiscoveryListState, uiConstraints: LayoutConstraints) {

    FullScreen(uiConstraints) {

        AppHeaderView(uiConstraints.appHeaderBarHeightDp){
            FeatureEntryPlaceholder()
            AppHeaderTitle(stringResource(R.string.discover_feature_desc), uiConstraints.headerWidthDp)
            HeaderSearchFeatureEntry()
        }

        AppBodyView(bodyHeightDp = {uiConstraints.appBodyHeightDp.dp}) {
            //https://developer.android.google.cn/jetpack/compose/lists
            LazyColumn(modifier = Modifier.fillMaxSize(),
                contentPadding = PaddingValues(horizontal = 0.dp,vertical = 8.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)){

                uiState.groupedTopics["V"]?.let {
                    item{
                        DiscoverEntryView(it.get(0)!!, uiConstraints)
                    }
                }

                var maxCount = 5
                uiState.groupedTopics.forEach { (key, topics) ->
                    if("V" != key && maxCount-- > 0) {
                        stickyHeader(key) {
                            Spacer(modifier = Modifier
                                .fillMaxWidth()
                                .height(10.dp)
                                .background(DemoTheme.colors.primary))
                        }
                        items(topics.subList(0,max(topics.size - 1,1))) { topic ->
                            DiscoverEntryView(topic, uiConstraints)
                        }
                    }
                }
            }
        }

        AppBottomNavigationView(DiscoverFeatureSDK.FEATURE_KEY)
    }
}
